package com.example.administrator.u17.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.example.administrator.u17.R;
import com.example.administrator.u17.adapter.OtherBaseAdapter;
import com.example.administrator.u17.model.ManHuaInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/15.
 */

public class OtherFragment extends Fragment {

    private GridView gv;
    private List<ManHuaInfo.Datas.Returns.Comicss> mData;
    private BaseAdapter mAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_other, null);
        initView(view);
        initData();
        setData();
        return view;
    }

    private void setData() {
        gv.setAdapter(mAdapter);

    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new OtherBaseAdapter(getActivity(), mData);
        loadData();
    }

    private void initView(View view) {
        gv = (GridView) view.findViewById(R.id.gv_other_fragment);
    }

    private void loadData() {
        String page = getArguments().getString("id");
        Log.e("TAG", "page: " + page);
        String path = "http://app.u17.com/v3/appV3_1/android/phone/comic/authorComic?user_id=" + page + "&v=3220102&key=8d5cdd4dcec75f94a41f69f5658e7fa225b37ead9c932da7c988bfa6b 8a59502f67cb41dbfed8353da3741ad7d569b7b1d10bf2c4ff5d6478b00e6224f8b427b7e80092215885 46188d28d387d2468dd2dc4240b571c7bd274167456195e0b8bc3bfca2140889de1%253A%253A %253Au17&model=8676-M01&come_from=openqq&android_id=d893eaa960d8a5e6";
        Log.e("TAG", "loadData: " + path);
        final RequestParams p = new RequestParams(path);
        x.http().get(p, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject object = new JSONObject(result);
                    JSONObject dataObject = object.getJSONObject("data");
                    JSONObject ReturnData = dataObject.getJSONObject("returnData");
                    JSONArray arry = ReturnData.getJSONArray("comics");
                    for (int i = 0; i < arry.length(); i++) {
                        ManHuaInfo.Datas.Returns.Comicss comicss = new ManHuaInfo.Datas.Returns.Comicss();
                        comicss.comicId = arry.getJSONObject(i).optString("comicId");
                        comicss.cover = arry.getJSONObject(i).optString("cover");
                        comicss.name = arry.getJSONObject(i).optString("name");
                        comicss.description = arry.getJSONObject(i).optString("description");
                        comicss.conTag = arry.getJSONObject(i).optInt("conTag");
                        comicss.tags = (List<String>) arry.getJSONObject(i).optJSONArray("tags");
                        comicss.author = arry.getJSONObject(i).optString("author");
                        mData.add(comicss);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.e("TAG", "onError: 错误");
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {
            }
        });

    }
}
